package com.tklab.smartcity.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.tklab.smartcity.App;
import com.tklab.smartcity.MainActivity;
import com.tklab.smartcity.R;
import com.tklab.smartcity.util.SpHelper;

import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WelcomeActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TextView tvView;
    private Button btnIndex;
    private RadioGroup rdGroup;
    private int[] rbs={R.id.rd1, R.id.rd2,R.id.rd3,R.id.rd4,R.id.rd5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_welcome);
        String first =SpHelper.getString("first");
        if("first".equals(first)){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }

        initView();
    }

    public void btnClickClick(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tvView = (TextView) findViewById(R.id.tvView);
        btnIndex = (Button) findViewById(R.id.btnIndex);
        rdGroup = (RadioGroup) findViewById(R.id.rdGroup);

        viewPager.setAdapter(new PagerAdapter() {
            private int[] imgs={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};

            @NonNull
            @NotNull
            @Override
            public Object instantiateItem(@NonNull @NotNull ViewGroup container, int position) {
                ImageView pic=new ImageView(getApplicationContext());
                pic.setScaleType(ImageView.ScaleType.CENTER_CROP);
                pic.setImageResource(imgs[position]);
                container.addView(pic);
                return pic;
            }

            @Override
            public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
                container.removeView((View)object);
            }

            @Override
            public int getCount() {
                return imgs.length;
            }

            @Override
            public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) {
                return object==view;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                rdGroup.check(rbs[position]);
                if (position+1==5){
                    tvView.setVisibility(View.VISIBLE);
                    btnIndex.setVisibility(View.VISIBLE);
                }else {
                    tvView.setVisibility(View.GONE);
                    btnIndex.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tvView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setIp();
            }
        });
    }

    private void setIp() {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("");
        View view=getLayoutInflater().inflate(R.layout.dialog_ip,null);
        final EditText txtIp=view.findViewById(R.id.txtIP);
        final EditText txtPort=view.findViewById(R.id.txtPort);
        builder.setView(view);
        builder.setPositiveButton("保存",null);
        final AlertDialog dialog=builder.create();
        dialog.show();

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isboolIp(txtIp.getText().toString())){
                    Toast.makeText(App.context,"IP格式错误",Toast.LENGTH_LONG).show();
                    txtIp.setFocusable(true);
                }else {
                    if ("".equals(txtPort.getText().toString())){
                        Toast.makeText(App.context,"端口号不为空",Toast.LENGTH_LONG).show();
                        txtIp.setFocusable(false);
                        txtPort.setFocusable(true);
                    }else {
                        String url="http://"+txtIp.getText().toString()+":"
                                +txtPort.getText().toString();
                        SpHelper.setString("url",url);
                        Log.d("TAG","onClick:"+SpHelper.getString("url"));
                        SpHelper.setString("first","first");
                        dialog.dismiss();
                    }
                }
            }
        });
    }
    private boolean isboolIp(String ipAddress) {

        String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pattern = Pattern.compile(ip);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }

}